package com.jzh.selfwallpaper.view.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.jzh.selfwallpaper.application.MyApplication;

@SuppressLint("AppCompatCustomView")
public class MoveEditText extends EditText {

    private int lastX = 0;
    private int lastY = 0;
    private int startX = 0;
    private int startY = 0;

    private Context context;
    private int screenWidth = MyApplication.SCREEN_WIDTH;
    private int screenHeight = MyApplication.SCREEN_HEIGHT;

    public MoveEditText(Context context) {
        super(context);
        this.context = context;
    }

    public MoveEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

   /* OnMoveEditTextClick onMoveEditTextClick;

    public void setOnMoveEditTextClick(OnMoveEditTextClick onMoveEditTextClick) {
        this.onMoveEditTextClick = onMoveEditTextClick;
    }

    public interface OnMoveEditTextClick{
        void onMoveEditTextClick();
    }*/

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = (int) event.getRawX();
                startY = (int) event.getRawY();
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) event.getRawX() - lastX;
                int dy = (int) event.getRawY() - lastY;

                int left = getLeft() + dx;
                int top = getTop() + dy;
                int right = getRight() + dx;
                int bottom = getBottom() + dy;
                if (left < 0) {
                    left = 0;
                    right = left + getWidth();
                }
                if (right > screenWidth) {
                    right = screenWidth;
                    left = right - getWidth();
                }
                if (top < 0) {
                    top = 0;
                    bottom = top + getHeight();
                }
                if (bottom > screenHeight) {
                    bottom = screenHeight;
                    top = bottom - getHeight();
                }
                layout(left, top, right, bottom);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                int dix = Math.abs((int) event.getRawX() - startX);
                int diy = Math.abs((int) event.getRawY() - startY);
                if (dix < 5 && diy < 5) {
                    this.setFocusable(true);
                    this.setFocusableInTouchMode(true);
                    this.requestFocus();
                    InputMethodManager inputManager=(InputMethodManager)this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(this,0);
                    this.setSelection(this.getText().length());
                    //Toast.makeText(context, "点击" + dix + " " + diy, Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
        return true;
    }

}
